//Component Object
import {
  invoiceReversal
} from "@src/api/invoice"
import router from '@src/router'
import {
  storeBindingsBehavior
} from "mobx-miniprogram-bindings";
import base from "@src/store/base";
Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor']
  }],
  properties: {
    popup: {
      type: Boolean,
      value: false,
      observer: function (val) {
        console.log('传过来的popup', val);
        if (val) {
          this.setData({
            ['isSelect[1]']: false,
            ['isSelect[2]']: false,
            ['isSelect[3]']: false,
            ['isSelect[4]']: false,
            redReason: ''
          })
          this.setData({
            showCover: val
          })
          setTimeout(() => {
            this.setData({
              showContent: val
            })
          }, 200)
        } else {
          this.setData({
            showContent: val
          })
          setTimeout(() => {
            this.setData({
              showCover: val
            })
          }, 200)
        }
      }
    },
    key: {
      type: Object,
      value: {}
    },

  },
  options: {
    multipleSlots: true
  },
  data: {
    showCover: false,
    showContent: false,
    isShow: false, //回调弹窗
    isSelect: {
      1: false,
      2: false,
      3: false,
      4: false
    },
    redReason: '',
    loading: false
  },
  methods: {
    close(e) {
      this.triggerEvent('onPopup', false);
    },
    // 取消
    cancel() {
      this.setData({
        isShow: false
      })
      this.triggerEvent('onPopup', false);
    },
    // 确定更改发票
    handerOk() {
      let params = []
      for (const key in this.data.isSelect) {
        params.push(this.data.isSelect[key])
      }
      if (params.indexOf(true) === -1) return wx.showToast({
        title: '请选择原因',
        icon: 'none',
        duration: 2000
      })
      if (this.data.isSelect[1]) {
        let data = Object.assign({}, this.data.key)
        router.push({
          path: 'code-order/pages/invoicing/main',
          params: {
            key: true
          },
          success: (res) => {
            res.eventChannel.emit('key', data)
          }
        })
        this.triggerEvent('onPopup', false);
      } else {
        if (this.data.isSelect[4] && !this.data.redReason) return wx.showToast({
          title: '具体原因不可为空',
          icon: 'none',
          duration: 2000
        })
        this.setData({
          showContent: false,
        })
        setTimeout(() => {
          this.setData({
            isShow: true
          })
        }, 200)
      }
    },
    // 二次弹窗确定
    repeatedlyOk() {
      this.setData({
        loading: true
      })
      wx.showLoading({
        title: '冲红中...',
      })
      invoiceReversal({
        fapiaoId: this.data.key.id,
        redReason: this.data.redReason
      }).then(res => {
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 2000
        })
        router.push({
          path: 'code-order/pages/invoiceHistory/main',
          params: {
            currentIndex: this.data.key.currentIndex
          }
        })
      }).finally(() => {
        wx.hideLoading()
        setTimeout(() => {
          this.setData({
            loading: false,
            isShow: false
          })
        }, 1000)

      })
      this.triggerEvent('onPopup', false);
    },
    onOver(e) {
      this.triggerEvent('onPopup', false);
    },
    changeCause(e) {
      const {
        cause
      } = e.currentTarget.dataset
      this.setData({
        ['isSelect[1]']: cause == 1,
        ['isSelect[2]']: cause == 2,
        ['isSelect[3]']: cause == 3,
        ['isSelect[4]']: cause == 4
      })
    }
  }
});